//
//  NetWorkingTool.swift
//  weibo
//
//  Created by 陈经纬 on 16/7/22.
//  Copyright © 2016年 陈经纬. All rights reserved.
//

import Foundation
import AFNetworking



class NetWorkingTool: AFHTTPSessionManager {
    static let shareInstance :  NetWorkingTool = {
        let baseURL = NSURL(string: "https://api.weibo.com/")
        let instance = NetWorkingTool(baseURL:baseURL,sessionConfiguration: NSURLSessionConfiguration.defaultSessionConfiguration())
        
        instance.responseSerializer.acceptableContentTypes = NSSet(objects:"application/json", "text/json", "text/javascript", "text/plain") as? Set
        
        return instance
    
    }()
    
    /**
     外部控制方法,发送网络请求
     
     */

    func loadStaues(finished:(arrarys :[[String:AnyObject]]?,error:NSError?)->())  {
        
        let path = "2/statuses/home_timeline.json"
        let parameters:[String:AnyObject] = ["access_token":(UserAccount.loadUserAccount()?.access_token)!]
        
        GET(path, parameters: parameters, success: { (task, objc) in
            
            guard let arr = ((objc as! [String:AnyObject])["statuses"] as? [[String:AnyObject]]) else{
                //如果没有微博数据:
                finished(arrarys:nil ,error:NSError(domain: "com.baidu.www", code: 100, userInfo: ["message":"没有获取到数据"]))
                return
            }
            
            //有微博数据:
            finished(arrarys: arr, error: nil)
            
            }) { (task, error) in
                finished(arrarys: nil, error: error)
                
        }
    }
}
